﻿package component.tree.web.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletHelper {
	private HttpServletRequest request;
	private HttpServletResponse response;
	
	public HttpServletRequest getRequest() {
		return request;
	}
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}
	public HttpServletResponse getResponse() {
		return response;
	}
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}
	
	
	public Map getCookies(HttpServletRequest request) {
		Map map = new HashMap();
		Cookie[] cookies = request.getCookies();
		if (cookies != null) {
			for (int i = 0; i < cookies.length; i++) {
				map.put(cookies[i].getName(), cookies[i]);
			}
		}

		return map;
	}

	// 在客户端记录登陆的次数
	public void setLoginCookie(HttpServletRequest request,
			HttpServletResponse response) {
		Map cookies = this.getCookies(request);
		Cookie cookie = (Cookie) cookies.get("loginCount");
		int count = 1;

		if (cookie == null) {// 第一次访问 就建立
			cookie = new Cookie("loginCount", "1");
		} else {
			count = Integer.parseInt(cookie.getValue());
			count++;
			cookie = new Cookie("loginCount", String.valueOf(count));
		}
		cookie.setMaxAge(300);
		response.addCookie(cookie);
	}
	
	//返回 D:\oldtfms1.0\rd057_西樵维护项目_intg\xiqiao_tfms\a.源代码\tfms\web\
	public static String getRealPath(HttpServletRequest request){
		String realPath = request.getRealPath("/");
		realPath = realPath.replace('\\','/');
		return realPath;
	}
	public static String getRealPath(HttpServletRequest request,boolean isReplace){
		String realPath = request.getRealPath("/");
		if(isReplace) realPath = realPath.replace('\\','/');
		return realPath;
	}
	
	//返回：http://localhost:8080/select
	public static String getBasePath(HttpServletRequest request) {
		String context = request.getContextPath();
		String basePath = request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + context;
		return basePath;
	}
	
	//返回：/open/component/tree.do
	public static String getPath(HttpServletRequest request) {		
		String path = request.getRequestURI();
		int i = path.indexOf("?");
		if(i!=-1){
			path = path.substring(0,i);
		}
		String context = request.getContextPath();
		path = path.substring(context.length());
		return path;
	}

	public Map getEnumeration(HttpServletRequest request) {
		Map map = new HashMap();
		Enumeration enumer = request.getParameterNames();
		while (enumer.hasMoreElements()) {
			String paramName = (String) enumer.nextElement();
			String paramValue = request.getParameter(paramName);
			map.put(paramName.trim(), paramValue.trim());
		}
		return map;
	}
	
	public void print(HttpServletRequest request,HttpServletResponse response, String msg) {
		if(request==null) request = this.request;
		if(response==null) response = this.response;
		//String charset = PropertiesUtil.getProperty("charset");
	    //response.setContentType("text/html;charset=" + charset); //设置输出编码格式
		response.setContentType("text/html;charset=GBK"); 
		try {
			request.setCharacterEncoding("GBK"); //设置输入编码格式
			response.getWriter().println(msg);
			//response.getWriter().flush();
			response.flushBuffer();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//设置返回客户端的响应
	public void write(HttpServletResponse response, String msg) {
		response.setContentType("text/xml");
		response.setCharacterEncoding("GBK");
		try {
			response.getWriter().write(msg);
			response.getWriter().flush();
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	//清除页面缓存
	public void setCache(HttpServletRequest request,HttpServletResponse response) {
		if(request==null) request = this.request;
		if(response==null) response = this.response;
		if (request.getProtocol().compareTo("HTTP/1.0") == 0) {
			response.setHeader("Pragma", "no-cache");
		}
		if (request.getProtocol().compareTo("HTTP/1.1") == 0) {
			response.setHeader("Cache-Control", "no-cache");
		}
		response.setDateHeader("Expires", 0);
	}
	
	
}
